package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.VersionRequest;
import com.ytjj.qmyx.admin.service.VersionService;
import com.ytjj.qmyx.mall.model.Version;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 版本管理
 */
@RestController
@RequestMapping("/version")
public class VersionController {

    @Autowired
    private VersionService versionService;


    /**
     * 查询所有版本
     * @param versionRequest
     * @return
     */
    @RequiresPermissions("version:list")
    @PostMapping("/list")
    public CommonResult list(@RequestBody VersionRequest versionRequest){
        CommonPage<Version> pageList = versionService.selectList(versionRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 编辑版本
     * @param version
     * @returnv
     */
    @RequiresPermissions("version:editVersion")
    @PostMapping("/editVersion")
    @OperLog(operModule = "版本模块", operDesc = "修改版本信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult edit(@RequestBody Version version){
        version.setUpdateTime(new Date());
        int count = versionService.edit(version);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 增加版本
     * @param version
     * @return
     */
    @RequiresPermissions("version:insertVersion")
    @PostMapping("/insertVersion")
    @OperLog(operModule = "版本模块", operDesc = "增加版本信息", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insert(@RequestBody Version version){
        version.setCreateTime(new Date());
        int count = versionService.insert(version);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

}
